package com.example.zhangwei.util;


import com.example.zhangwei.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class UtilMethod {
	
	private static UtilMethod instance;
	
	/**
     * <默认构造函数>
     */
    private UtilMethod()
    {
    }

    /**
     * getInstance <功能详细描述>
     * 
     * @return getInstance
     * @see [类、类#方法、类#成员]
     */
    public static synchronized UtilMethod getInstance()
    {
    	if (instance == null)
    	{
    		instance = new UtilMethod();
    	}
    	return instance;
    }
    
    
    
    /**
     * 判断网络是否正常
     * 
     * @param context Context
     * @return true 网络ok，false 无网络
     */
    public boolean isNetWorkOk(Context context)
    {
        return getConnectedNetworkInfo(context) != null;

    }
    
    private NetworkInfo getConnectedNetworkInfo(Context context)
    {
        try
        {
            ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity == null)
            {
            	
            }
            else
            {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
                if (info != null)
                {
                    for (int i = 0; i < info.length; i++)
                    {
                        if (info[i].getState() == NetworkInfo.State.CONNECTED)
                        {
                            return info[i];
                        }
                    }
                }
            }

        }
        catch (Exception e)
        {
            
        }
        return null;
    }
    
    
    /**
     * 返回按钮退出系统
     * 
     * @param activity Activity
     */
    public void showDialogBack(final Activity activity)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(activity.getString(R.string.dialog_title));
        builder.setMessage("   " + activity.getString(R.string.STR_EXIT_CONFIRM));
        builder.setPositiveButton(activity.getString(R.string.confirm), new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int whichButton)
            {
            	activity.finish();
            }
        });
        builder.setNeutralButton(activity.getString(R.string.ingron), new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int whichButton)
            {
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(startMain);
            }
        });
        builder.setNegativeButton(activity.getString(R.string.cancel), null);
        // builder.setIcon(Color.parseColor("#FF0000"));
        AlertDialog dlg = builder.create();
        dlg.show();
    }

}
